package com.cookman.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.cookman.utils.Constants;

/**
 * Created by Cookman on 14.04.14.
 */
public class MenuScreen extends AbstractGameScreen {

    private Stage stage;
    private Skin skinMenu;

    private Button btnMenuPlay;
    private Button btnMenuExit;


    public MenuScreen(Game game) {
        super(game);
    }

    @Override
    public void render(float deltaTime) {
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(deltaTime);
        stage.draw();
        Table.drawDebug(stage);
    }

    @Override
    public void resize(int width, int height) {
        FitViewport viewport=new FitViewport(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT);
        stage.setViewport(viewport);
    }

    @Override
    public void show() {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);
        rebuildStage();
    }

    private void startGame() {
      //  game.setScreen(new GameScreen(game));
    }

    private void rebuildStage() {
        skinMenu = new Skin(Gdx.files.internal(Constants.SKIN_UI), new TextureAtlas(Constants.PACK_UI_PATH));
        stage.clear();
        Stack stack = new Stack();
        stage.addActor(stack);
        stack.setSize(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT);
        stack.add(buildMenuButtons());
    }

    private Table buildMenuButtons() {
        Table table = new Table();
        btnMenuPlay = new Button(skinMenu, "start");
        table.add(btnMenuPlay);
        btnMenuPlay.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                startGame();
            }
        });
        table.row();
        btnMenuExit = new Button(skinMenu, "exit");
        table.add(btnMenuExit);
        btnMenuExit.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                System.exit(0);
            }
        });

        return table;
    }

    @Override
    public void hide() {
        stage.dispose();
        skinMenu.dispose();
    }

    @Override
    public void pause() {
    }
}
